/**
 * 
 */
package valfazz.android.karfinder.activity;

import java.util.ArrayList;

import valfazz.android.karfinder.R;
import valfazz.android.karfinder.database.DatabaseHelper;
import valfazz.android.karfinder.pojo.SpinnerItem;
import valfazz.android.karfinder.util.SongListView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * @author LuChanThanh
 * 
 */
public class SongActivity extends Activity {
	private static final String TAG = "SongActivity";
	private String[] charList = { "*", "#", "A", "B", "C", "D", "E", "F", "G",
			"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "S", "Y", "Z" };

	private DatabaseHelper dbHelper = null;
	private Spinner spnVolume, spnChar;
	private String charTemp = "";
	private String query = "";
	private ArrayList<SpinnerItem> spnList;
	private SongListView listView;
	private SpinnerAdapter spinnerAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_songs);

		listView = new SongListView(this, R.id.lvSongList);
		dbHelper = new DatabaseHelper(this);
		spnList = dbHelper.fetchSpinnerList();
		//Log.i("spnList length", spnList.size() + "");
		spnVolume = (Spinner) findViewById(R.id.spnVolume);

		spinnerAdapter = new SpinnerAdapter(this, android.R.layout.simple_spinner_item);
		//spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnVolume.setAdapter(spinnerAdapter);
		spinnerAdapter.notifyDataSetChanged();

		spnChar = (Spinner) findViewById(R.id.spnChar);
		ArrayAdapter<String> adapterChar = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, charList);
		adapterChar.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnChar.setAdapter(adapterChar);

		spnVolume.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> list, View arg1,
					int position, long id) {
				query = "select SONG_ID, SONG_TITLE, SONG_LYRICS from SONG where ";
				String strTypeItem = spnList.get(position).getType();
				String strValueItem = spnList.get(position).getValueItem();

				if (strTypeItem == "vol") {
					query += "SONG_VOLUME=" + strValueItem;
				} else if (strTypeItem == "lan") {
					query += "SONG_LANGUAGE='" + strValueItem + "'";
				} else if (strTypeItem == "gen") {
					query += "SONG_GENRE='" + strValueItem + "'";
				}

				String charTemp = spnChar.getSelectedItem().toString();
				String strQuery = query;
				if (!charTemp.equals("*")) {
					if (charTemp.equals("#")) {
						strQuery += " and (TEN_BAI_HAT LIKE '0%'";
						for (int i = 1; i < 10; i++) {
							strQuery += " or TEN_BAI_HAT LIKE '" + i + "%'";
						}
						strQuery += ")";
					} else
						strQuery += " and TEN_BAI_HAT LIKE '" + charTemp + "%'";
				}

				Log.i("Truy Van: ", strQuery);
				listView.setQuery(strQuery);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		spnChar.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				charTemp = spnChar.getItemAtPosition(pos) + "";
				String strQuery = query;
				//Log.i("Truy van bien query: ", query);
				if (!charTemp.equals("*")) {
					if (charTemp.equals("#")) {
						strQuery += " and (TEN_BAI_HAT LIKE '0%'";
						for (int i = 1; i < 10; i++) {
							strQuery += " or TEN_BAI_HAT LIKE '" + i + "%'";
						}
						strQuery += ")";
					} else
						strQuery += " and TEN_BAI_HAT LIKE '" + charTemp + "%'";
				}
				//Log.i("Truy Van prefix: ", strQuery);
				listView.setQuery(strQuery);

			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}

		});
	}
	
	@Override
	protected void onPause() {
		Log.i(TAG, "onPause");
		super.onPause();
		dbHelper.close();
		dbHelper = null;
	}

	@Override
	protected void onResume() {
		Log.i(TAG, "onResume");
		super.onResume();
		//spnList.clear();
		if (dbHelper == null)
			dbHelper = new DatabaseHelper(this);
		spnList = dbHelper.fetchSpinnerList();
		spinnerAdapter.notifyDataSetChanged();
		//spnVolume.setAdapter(spinnerAdapter);
	}
	
	public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {
		/**
		 * @param context
		 * @param textViewResourceId
		 * @param objects
		 */
		public SpinnerAdapter(Context context, int textViewResourceId) {
			super(context, textViewResourceId, spnList);
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			TextView label = new TextView(SongActivity.this);
			label.setTextColor(Color.BLACK);
			label.setText(spnList.get(position).getDisplayItem());

			return label;

		}

		@Override
		public View getDropDownView(int position, View convertView,
				ViewGroup parent) {
			TextView label = new TextView(SongActivity.this);
			label.setTextColor(Color.BLACK);
			label.setPadding(10, 12, 10, 12);
			label.setText(spnList.get(position).getDisplayItem());

			return label;
		}

	}
}
